package cn.tedu.hotel.controller;

import cn.tedu.hotel.pojo.dto.CustomerAddNewDTO;
import cn.tedu.hotel.pojo.vo.CustomerListItemVO;
import cn.tedu.hotel.pojo.vo.CustomerStandardVO;
import cn.tedu.hotel.security.LoginPrincipal;
import cn.tedu.hotel.service.ICustomerService;
import cn.tedu.hotel.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 *  处理客户相关的控制器类
 */
@Api(tags = "10. 客户管理模块")
@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    public CustomerController(){
        log.debug("创建控制器：CustomerController");
    }

    @ApiOperation("添加客户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(CustomerAddNewDTO customerAddNewDTO){
        log.debug("开始处理【添加客户】的请求，参数：{}",customerAddNewDTO);
        customerService.addNew(customerAddNewDTO);
        return JsonResult.ok();
    }


    @ApiOperation("根据id删除客户")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("开始处理【根据id删除客户】的请求，id= {}",id);
        customerService.deleteById(id);
        return JsonResult.ok();
    }


    @ApiOperation("标记删除客户信息")
    @ApiOperationSupport(order = 201)
    @PreAuthorize("hasAuthority('/hms/admin/update')")
    @PostMapping("/{id:[0-9]+}/is-delete")
    public JsonResult<Void> setIsDelete(@PathVariable Long id,
                                       @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("准备处理【标记删除客户】的请求：id={}", id);
        log.debug("当前登录的用户（当事人）的id：{}", loginPrincipal.getId());
        log.debug("当前登录的用户（当事人）的用户名：{}", loginPrincipal.getUsername());
        customerService.setIsDelete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询客户信息")
    @ApiOperationSupport(order = 300)
    @GetMapping("/get/standard")
    public JsonResult<CustomerStandardVO> getStandard(@Param("id") Long id,
                                        @Param("userNumber") String userNumber,
                                        @Param("userName") String userName,
                                        @Param("idNumber") String idNumber,
                                        @Param("phone") String phone,
                                       @Param("isDelete") Integer isDelete){
        log.debug("开始处理【查询客户】的请求");
        CustomerStandardVO standardCustomer =
                customerService.getStandardCustomer(id, userNumber, userName, idNumber, phone,isDelete);
        return JsonResult.ok(standardCustomer);
    }


    @ApiOperation("查询客户列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("/list")
    public JsonResult<List<CustomerListItemVO>> listCustomers(
              @Param("id") Long id,
              @Param("userNumber") String userNumber,
              @Param("userName") String userName,
              @Param("idNumber") String idNumber,
              @Param("phone") String phone,
//              @Param("isDelete") Integer isDelete
              @RequestParam(required = false,defaultValue = "1") Integer isDelete
              ){
        log.debug("开始处理【查询部分客户列表】的请求");
        List<CustomerListItemVO> customerListItemVOS =
                customerService.listCustomers(id, userNumber, userName, idNumber, phone,isDelete);
        return JsonResult.ok(customerListItemVOS);
    }


}
